package cn.shengchao.wecom.wxcp.acl.adapters.repositories;

import cn.hutool.core.collection.CollUtil;
import cn.shengchao.wecom.wxcp.acl.ports.repositories.ICpExternalInviteRepository;
import cn.shengchao.wecom.wxcp.infrastructure.dao.CpExternalInviteDao;
import cn.shengchao.wecom.wxcp.infrastructure.dao.po.CpExternalInvite;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: examstarapi
 * @description: 成员外联关系
 * @author: Xiong weiqiang
 * @create: 2025-04-29 15:12
 */
@Service
@RequiredArgsConstructor
public class CpExternalInviteRepository implements ICpExternalInviteRepository {
    private final CpExternalInviteDao cpExternalInviteDao;
    @Override
    public void createExternalInvite(String userId, String externalUserId, String state) {
        CpExternalInvite externalInvite = new CpExternalInvite();
        externalInvite.setUserId(userId);
        externalInvite.setExternalUserId(externalUserId);
        externalInvite.setAddTime(new Date());
        externalInvite.setState(state);
        externalInvite.setAsDelete(0);
        cpExternalInviteDao.insert(externalInvite);
    }
    @Override
    public void deleteExternalInvite(String userId, String externalUserId) {
        List<CpExternalInvite> externalInviteList = cpExternalInviteDao.selectList(new LambdaQueryWrapper<>(CpExternalInvite.class)
                .eq(CpExternalInvite::getAsDelete, 0)
                .eq(CpExternalInvite::getUserId, userId).eq(CpExternalInvite::getExternalUserId, externalUserId));
        if (CollUtil.isNotEmpty(externalInviteList)) {
            externalInviteList.forEach(externalInvite -> {
                externalInvite.setAsDelete(1);
                externalInvite.setDeleteTime(new Date());
                cpExternalInviteDao.updateById(externalInvite);
            });
        }
    }
}